package com.app.common.view.nav.tabnav;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import commonlibrary.R;

import com.app.common.adapter.UIFragmentPagerAdapter;
import com.app.common.obj.UIFrameworkContent;
import com.app.common.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zhoulikai on 17/2/16.
 * tab 导航类
 */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class TabNavigationBar extends LinearLayout {

    private static final int UP = 1;
    private static final int DOWN = 2;
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private int tabItemLayoutId = -1;
    private LayoutInflater inflater;

    private final int TABITEM_HEIGHT = 50;

    private int tabItemTextColor;
    private int tabItemTextSize;
    private List<UIFrameworkContent> uiFrameworkContents = new ArrayList<>();


    public TabNavigationBar(Context context) {
        this(context, null);
    }

    public TabNavigationBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabNavigationBar(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public TabNavigationBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setOrientation(LinearLayout.VERTICAL);
        initAttrValue(context, attrs);
    }

    private void initAttrValue(Context mContext, AttributeSet attrs) {
        TypedArray a = null;
        if (mContext == null || attrs == null) {
            return;
        }
        try {
            a = mContext.obtainStyledAttributes(attrs, R.styleable.TabNavigationBar);
            int direction = a.getInt(R.styleable.TabNavigationBar_direction, UP);
            int contentViewpageId = a.getInt(R.styleable.TabNavigationBar_contentViewpageLayoutID, -1);
            int tablayoutId = a.getInt(R.styleable.TabNavigationBar_tablayoutID, -1);
            int tabItemHeight = a.getInt(R.styleable.TabNavigationBar_tabItemHeight, -1);

            tabItemTextColor = a.getInt(R.styleable.TabNavigationBar_tabItemTextColor, -1);
            tabItemTextSize = a.getInt(R.styleable.TabNavigationBar_tabItemTextSize, -1);

            int tablayoutBackGroundColor = a.getColor(R.styleable.TabNavigationBar_tablayoutBackGroundColor, -1);

            //内容区viewpager
            if (contentViewpageId == -1) {
                contentViewpageId = R.layout.default_contentpage;
            }

            //导航tab使用tablayout
            if (tablayoutId == -1) {
                tablayoutId = R.layout.default_tablayout;
            }

            inflater = LayoutInflater.from(mContext);
            if (inflater == null) {
                return;
            }

            if (tabItemHeight == -1) {
                tabItemHeight = TABITEM_HEIGHT;
            }

            //实例化布局，根据方向并添加到布局中
            viewPager = (ViewPager) inflater.inflate(contentViewpageId, null);
            tabLayout = (TabLayout) inflater.inflate(tablayoutId, null);

            if (tablayoutBackGroundColor != -1) {
                tabLayout.setBackgroundColor(tablayoutBackGroundColor);
            }


            LinearLayout.LayoutParams tabLayoutParms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(mContext, tabItemHeight));
            LinearLayout.LayoutParams contentLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

//            viewPager.setOnTouchListener(new OnTouchListener() {
//                @Override
//                public boolean onTouch(View view, MotionEvent motionEvent) {
//                    return true;
//                }
//            });

            if (direction == UP) {
                contentLayoutParams.weight = 1;
                addView(tabLayout, tabLayoutParms);
                addView(viewPager, contentLayoutParams);
            } else if (direction == DOWN) {
                contentLayoutParams.weight = 1;
                addView(viewPager, contentLayoutParams);
                addView(tabLayout, tabLayoutParms);
            }

            //tab item布局
            tabItemLayoutId = a.getInt(R.styleable.TabNavigationBar_tabItemLayoutID, -1);
            if (tabItemLayoutId == -1) {
                tabItemLayoutId = R.layout.default_tablayoutitem;
            }

        } finally {
            if (a != null) {
                a.recycle();
            }
        }

    }

    public void setContent(FragmentActivity activity, List<UIFrameworkContent> uiFrameworkContents) {
        if (activity == null || uiFrameworkContents == null) {
            return;
        }

        this.uiFrameworkContents = uiFrameworkContents;

        viewPager.setAdapter(new UIFragmentPagerAdapter(activity.getSupportFragmentManager(), uiFrameworkContents));
        viewPager.setCurrentItem(0);
//        tabLayout.setupWithViewPager(viewPager);
//        tabLayout.setSelected(false);
        tabLayout.setOnTabSelectedListener(null);
        for (int i = 0; i < this.uiFrameworkContents.size(); i++) {
            UIFrameworkContent uiFrameworkContent = this.uiFrameworkContents.get(i);
            if (uiFrameworkContent != null) {
                View tabItemLayout = inflater.inflate(tabItemLayoutId, null);
                ImageView iconView = (ImageView) tabItemLayout.findViewById(R.id.nav_icon);
                iconView.setBackgroundResource(uiFrameworkContent.getBackgroundId());

                TextView titleView = (TextView) tabItemLayout.findViewById(R.id.nav_title);
                titleView.setText(uiFrameworkContent.getTitle());
                if (tabItemTextColor != -1) {
                    titleView.setTextColor(tabItemTextColor);
                }

                if (tabItemTextSize != -1) {
                    titleView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, tabItemTextSize);
                }
//                tabItemLayout.setOnClickListener(new ItemClick(i));
                tabLayout.addTab(tabLayout.newTab().setCustomView(tabItemLayout));

            }
        }

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition(), false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }

    class ItemClick implements OnClickListener {
        private int postion;

        ItemClick(int postion) {
            this.postion = postion;
        }

        @Override
        public void onClick(View view) {
            viewPager.setCurrentItem(postion);
            for (int i = 0; i < tabLayout.getTabCount(); i++) {
                if (i == postion) {
                    tabLayout.getTabAt(postion).getCustomView().setSelected(true);
                } else {
                    tabLayout.getTabAt(i).getCustomView().setSelected(false);
                }
            }

        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

}
